package cn.baage.campus.config.security.auth;

import cn.hutool.core.lang.Assert;
import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;

import java.util.Collection;


/**
 * @Description: 微信登录 AuthenticationToken
 * 用于在认证过程中存储主体的各种基本信息（例如：用户名，密码，所属部门等等）和主体的认证信息（例如，接口权限）
 * @Author: 八阿哥
 * @url: baage.cn
 * @Version: 1.0
 **/
public class WxAuthenticationToken extends AbstractAuthenticationToken {


    // 存储主体信息
   private final Object principal;

   // 储存验证主体信息的凭据
   private Object credentials = "wx";


   public WxAuthenticationToken(Object principal) {
       super(null);
       this.principal = principal;
       setAuthenticated(false);
   }

   public WxAuthenticationToken(Object principal, Collection<? extends GrantedAuthority> authorities) {
       super(authorities);
       this.principal = principal;
       super.setAuthenticated(true);
   }

   @Override
   public Object getCredentials() {
       return this.credentials;
   }

   @Override
   public Object getPrincipal() {
       return this.principal;
   }

   @Override
   public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {
       // 禁止修改 Authenticated 状态
       Assert.isTrue(!isAuthenticated,
               "Cannot set this token to trusted - use constructor which takes a GrantedAuthority list instead");
       super.setAuthenticated(false);
   }

   @Override
   public void eraseCredentials() {
       super.eraseCredentials();
       this.credentials = null;
   }

}